/**
 * 底栏仪表盘
 * "board_2018_b169"
 * 
 * @author Zdream
 * @date 2020-04-15
 * @since aurora 0.4.0
 */

function create (context) {
	return {
		template : context.template,
		data : function () {
			return {
				// 计时器显示时间
				timerText : '',
				// 各选手的时间
				p1Score : '',
				p2Score : '',
				p3Score : '',
				p4Score : '',
				// edit 部分
				title : '',
				editP1Name : '1P',
				editP2Name : '2P',
				editP3Name : '3P',
				editP4Name : '4P',
				editP1Preset : 0,
				editP2Preset : 0,
				editP3Preset : 0,
				editP4Preset : 0,
				// 状态数据
				status : null,
			}
		},
		props : {
			sid : String,
		},
		methods : {
			// 事件触发
			editPlayerNameSubmit : function (index) {
				var d = {};
				d['p' + index + 'Name'] = this['editP' + index + 'Name'];
				context.sendCommand(this.sid, 'player_name_change', d);
			},
			editPlayerPresetSubmit : function (index) {
				var d = {
					player : index,
					score : this['editP' + index + 'Preset']
				};
				context.sendCommand(this.sid, 'preset_score', d);
			},
			editTitleSubmit : function () {
				var d = {};
				d['title'] = this.title;
				context.sendCommand(this.sid, 'title_change', d);
			},
			timerStart : function () {
				context.sendCommand(this.sid, 'timer_start', null);
			},
			timerPause : function () {
				context.sendCommand(this.sid, 'timer_pause', null);
			},
			timerStop : function () {
				context.sendCommand(this.sid, 'timer_stop', null);
			},
			timerReset : function () {
				context.sendCommand(this.sid, 'timer_reset', null);
			},
			timerUndoPause : function () {
				context.sendCommand(this.sid, 'timer_undoPause', null);
			},
			playerRecord : function (index) {
				context.sendCommand(this.sid, 'timer_record', {player : index});
			},
			playerUndoRecord : function (index) {
				context.sendCommand(this.sid, 'timer_undoRecord', {player : index});
			},
			// 其它
			onScreenUpdated : function () {
				let status = this.status = context.getStatus(this.sid);
				if (!status || !status.data) {
					return;
				}
				let data = status.data;
		
				// timerText
				if (data.timer.playing && data.timer.pause) {
					this.timerText = msToText(data.timer.pause); // 传过来是毫秒
				} else {
					this.timerText = '';
				}
		
        // playerScore
        this.p1Score = (data.screen.p1Score) ? msToText(data.screen.p1Score) : '';
        this.p2Score = (data.screen.p2Score) ? msToText(data.screen.p2Score) : '';
        this.p3Score = (data.screen.p3Score) ? msToText(data.screen.p3Score) : '';
        this.p4Score = (data.screen.p4Score) ? msToText(data.screen.p4Score) : '';
			},
		},
		mounted : function () {
			this.onScreenUpdated();
			context.getBus().$on('screen-status-sync-all', this.onScreenUpdated);
			context.getBus().$on('screen-updated', this.onScreenUpdated);
			context.getBus().$on('screen-destroyed', this.onScreenUpdated);
		},
    destroyed : function () {
      context.getBus().$off('screen-status-sync-all', this.onScreenUpdated);
      context.getBus().$off('screen-updated', this.onScreenUpdated);
      context.getBus().$off('screen-destroyed', this.onScreenUpdated);
    }
	};
}

/**
 * 将毫秒数据转成文本
 * @param {number} ms
 * @returns {string}
 */
function msToText (ms) {
	if (ms < 0) {
		return '<播放中>'
	}

	let nms = ms % 1000;
	let tms;
	if (nms < 10) {
		tms = '00' + nms;
	} else if (nms < 100) {
		tms = '0' + nms;
	} else {
		tms = '' + nms;
	}

	// s
	let remain = parseInt(ms / 1000);
	if (remain < 60) {
		return remain + '.' + tms;
	}
	let ns = remain % 60;
	let ts;
	if (ns < 10) {
		ts = '0' + ns;
	} else {
		ts = '' + ns;
	}

	// m
	remain = parseInt(remain / 60);
	if (remain < 60) {
		return remain + ':' + ts + '.' + tms;
	}
	let nm = remain % 60;
	let tm;
	if (nm < 10) {
		tm = '0' + nm;
	} else {
		tm = '' + nm;
	}

	// h
	remain = parseInt(remain / 60);
	return remain + ':' + ms + ':' + ts + '.' + tms;
}

export default create;
